package com.mall.server.controller.yxcontroller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.server.service.HomeService;
import com.tanhua.model.domain.*;
import com.tanhua.model.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/home")
public class HomeController {

    @Autowired
    private HomeService homeService;

    /**
     * 分页查询推荐品牌
     * @param brandName
     * @param recommendStatus
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/brand/list")
    public R brandList(String brandName,Integer recommendStatus,
                       @RequestParam(defaultValue = "1")Integer pageNum,
                       @RequestParam(defaultValue = "5")Integer pageSize){
        Page<SmsHomeBrand> page = new Page<>(pageNum,pageSize);
        PageVo pageVo =homeService.brandList(brandName,recommendStatus,page);
        return R.success(pageVo);
    }

    /**
     * 批量修改推荐品牌状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/brand/update/recommendStatus")
    public R updateRecommendStatus(@RequestParam List<Long> ids,Integer recommendStatus){
        homeService.updateRecommendStatus(ids,recommendStatus);
        return R.success("");
    }

    /**
     * 修改推荐品牌排序
     * @param smsHomeBrand
     * @return
     */
    @PostMapping("/brand/update/sort/{id}")
    public R updateSort( SmsHomeBrand smsHomeBrand){
        if (smsHomeBrand ==null || smsHomeBrand.getId() ==null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        homeService.updateSort(smsHomeBrand);
        return R.success("");
    }

    /**
     * 添加首页推荐品牌
     * @param smsHomeBrand
     * @return
     */
    @PostMapping("/brand/create")
    public R create(@RequestBody List<SmsHomeBrand> smsHomeBrand){
        if (smsHomeBrand ==null ){
            throw new BusinessException(ErrorResult.parameterError());
        }
        homeService.create(smsHomeBrand);
        return R.success("");
    }


    /**
     * 批量删除推荐品牌
     * @param ids
     * @return
     */
    @PostMapping("/brand/delete")
    public R delete(@RequestParam List<Integer> ids){
        if (ids ==null ){
            throw new BusinessException(ErrorResult.parameterError());
        }
        homeService.delete(ids);
        return R.success("");
    }


//============================================新品推荐
    /**
     * 分页查询首页新品
     * @param productName
     * @param recommendStatus
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/newProduct/list")
    public R newProductList(String productName,Integer recommendStatus,
                            @RequestParam(defaultValue = "1")Integer pageNum,
                            @RequestParam(defaultValue = "5")Integer pageSize){
        Page<SmsHomeNewProduct> page = new Page<>(pageNum,pageSize);
        PageVo pageVo =homeService.newProductList(productName,recommendStatus,page);
        return R.success(pageVo);
    }


    /**
     *  添加首页新品
     * @param smsHomeNewProduct
     * @return
     */
    @PostMapping("/newProduct/create")
    public R createNewProduct(@RequestBody List<SmsHomeNewProduct> smsHomeNewProduct){
        if (smsHomeNewProduct ==null ){
            throw new BusinessException(ErrorResult.parameterError());
        }
        homeService.createNewProduct(smsHomeNewProduct);
        return R.success("");
    }

    /**
     * 批量删除首页新品
     * @param ids
     * @return
     */
    @PostMapping("/newProduct/delete")
    public R deleteNewProduct(@RequestParam List<Integer> ids){
        if (ids ==null ){
            throw new BusinessException(ErrorResult.parameterError());
        }
        homeService.deleteNewProduct(ids);
        return R.success("");
    }

    /**
     * 批量修改首页新品状态   是否推荐
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/newProduct/update/recommendStatus")
    public R updateNewProductRecommendStatus(@RequestParam List<Long> ids,Integer recommendStatus){
        if (ids ==null  || recommendStatus ==null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        homeService.updateNewProductRecommendStatus(ids,recommendStatus);
        return R.success("");
    }

    /**
     * 修改首页新品排序
     * @param smsHomeNewProduct
     * @return
     */
    @PostMapping("/newProduct/update/sort/{id}")
    public R updateNewProductSort(SmsHomeNewProduct smsHomeNewProduct){
        homeService.updateNewProductSort(smsHomeNewProduct);
        return R.success("");
    }

    //============================================人气推荐

    /**
     * 分页查询人气推荐
     * @param productName
     * @param recommendStatus
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/recommendProduct/list")
    public R recommendProductList(String productName,Integer recommendStatus,
                                  @RequestParam(defaultValue = "1")Integer pageNum,
                                  @RequestParam(defaultValue = "5")Integer pageSize){
        Page<SmsHomeRecommendProduct> page = new Page<>(pageNum,pageSize);
        PageVo pageVo =homeService.recommendProductList(productName,recommendStatus,page);
        return R.success(pageVo);
    }

    /**
     * 添加人气推荐
     * @param smsHomeRecommendProduct
     * @return
     */
    @PostMapping("/recommendProduct/create")
    public R createRecommendProduct(@RequestBody List<SmsHomeRecommendProduct> smsHomeRecommendProduct){
        if (smsHomeRecommendProduct ==null ){
            throw new BusinessException(ErrorResult.parameterError());
        }
        homeService.createRecommendProduct(smsHomeRecommendProduct);
        return R.success("");
    }

    /**
     * 批量删除人气推荐
     * @param ids
     * @return
     */
    @PostMapping("/recommendProduct/delete")
    public R deleteRecommendProduct(@RequestParam List<Integer> ids){
        if (ids ==null ){
            throw new BusinessException(ErrorResult.parameterError());
        }
        homeService.deleteRecommendProduct(ids);
        return R.success("");
    }

    /**
     * 批量修改人气推荐   是否推荐
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/recommendProduct/update/recommendStatus")
    public R updateRecommendProductRecommendStatus(@RequestParam List<Long> ids,Integer recommendStatus){
        if (ids ==null  || recommendStatus ==null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        homeService.updateRecommendProductRecommendStatus(ids,recommendStatus);
        return R.success("");
    }

    /**
     * 修改人气排序
     * @param smsHomeRecommendProduct
     * @return
     */
    @PostMapping("/recommendProduct/update/sort/{id}")
    public R updateRecommendProductSort(SmsHomeRecommendProduct smsHomeRecommendProduct){
        homeService.updateRecommendProductSort(smsHomeRecommendProduct);
        return R.success("");
    }


    //============================================专题推荐
    /**
     * 分页查询专题推荐
     * @param subjectName
     * @param recommendStatus
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/recommendSubject/list")
    public R recommendSubjectList(String subjectName,Integer recommendStatus,
                                  @RequestParam(defaultValue = "1")Integer pageNum,
                                  @RequestParam(defaultValue = "5")Integer pageSize){
        Page<SmsHomeRecommendSubject> page = new Page<>(pageNum,pageSize);
        PageVo pageVo =homeService.recommendSubjectList(subjectName,recommendStatus,page);
        return R.success(pageVo);
    }


    /**
     * 添加首页推荐专题
     * @param smsHomeRecommendSubject
     * @return
     */
    @PostMapping("/recommendSubject/create")
    public R recommendSubjectCreate(@RequestBody List<SmsHomeRecommendSubject> smsHomeRecommendSubject){
        homeService.recommendSubjectCreate(smsHomeRecommendSubject);
        return R.success("");
    }


    /**
     * 批量删除专题推荐
     * @param ids
     * @return
     */
    @PostMapping("/recommendSubject/delete")
    public R deleteRecommendSubject(@RequestParam List<Integer> ids){
        if (ids ==null ){
            throw new BusinessException(ErrorResult.parameterError());
        }
        homeService.deleteRecommendSubject(ids);
        return R.success("");
    }

    /**
     * 批量修改专题推荐   是否推荐
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/recommendSubject/update/recommendStatus")
    public R updateRecommendSubjectRecommendStatus(@RequestParam List<Long> ids,Integer recommendStatus){
        if (ids ==null  || recommendStatus ==null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        homeService.updateRecommendSubjectRecommendStatus(ids,recommendStatus);
        return R.success("");
    }

    /**
     * 修改专题推荐排序
     * @param smsHomeRecommendSubject
     * @return
     */
    @PostMapping("/recommendSubject/update/sort/{id}")
    public R updateRecommendSubjectSort(SmsHomeRecommendSubject smsHomeRecommendSubject){
        homeService.updateRecommendSubjectSort(smsHomeRecommendSubject);
        return R.success("");
    }

    //============================================广告列表

    /**
     * 查看广告列表
     * @param endTime
     * @param type
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/advertise/list")
    public R advertiseList(String endTime,Integer type,String name,
                           @RequestParam(defaultValue = "1")Integer pageNum,
                           @RequestParam(defaultValue = "5")Integer pageSize){
        Page<SmsHomeAdvertise> page = new Page<>(pageNum,pageSize);
        PageVo pageVo =homeService.advertiseList(endTime,type,name,page);
        return R.success(pageVo);
    }

    /**
     * 添加广告
     * @param smsHomeAdvertise
     * @return
     */
    @PostMapping("/advertise/create")
    public R advertiseCreate(@RequestBody SmsHomeAdvertise smsHomeAdvertise){
        if (smsHomeAdvertise ==null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        homeService.advertiseCreate(smsHomeAdvertise);
        return R.success("");
    }

    /**
     * 批量删除广告列表
     * @param ids
     * @return
     */
    @PostMapping("/advertise/delete")
    public R deleteAdvertise(@RequestParam List<Integer> ids){
        if (ids ==null ){
            throw new BusinessException(ErrorResult.parameterError());
        }
        homeService.deleteAdvertise(ids);
        return R.success("");
    }

    /**
     * 修改广告上下线状态
     * @param
     * @return
     */
    @PostMapping("/advertise/update/status/{id}")
    public R updateAdvertiseStatus(@PathVariable("id")Long id,Integer status){
        SmsHomeAdvertise smsHomeAdvertise = new SmsHomeAdvertise();
        smsHomeAdvertise.setId(id);
        smsHomeAdvertise.setStatus(status);
        if (smsHomeAdvertise ==null  || smsHomeAdvertise.getId() ==null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        homeService.updateAdvertiseStatus(smsHomeAdvertise);
        return R.success("");
    }


    /**
     * 修改广告
     * @param smsHomeAdvertise
     * @return
     */
    @PostMapping("/advertise/update/{id}")
    public R updateAdvertise(@RequestBody SmsHomeAdvertise smsHomeAdvertise){
        if (smsHomeAdvertise ==null  ){
            throw new BusinessException(ErrorResult.parameterError());
        }
        homeService.updateAdvertise(smsHomeAdvertise);
        return R.success("");
    }

    /**
     * 获取广告详情
     * @param id
     * @return
     */
    @GetMapping("/advertise/{id}")
    public R getAdvertiseById(@PathVariable("id") Long id){
        if (id ==null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        SmsHomeAdvertise smsHomeAdvertise =homeService.getAdvertiseById(id);
        return R.success(smsHomeAdvertise);
    }



}
